﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

using MySql.Data.MySqlClient;

namespace MakeIT.MiraPark.Negocios
{
    public static partial class ModelBO
    {
        public static CorVO getCor(int idCor)
        {
            List<CorVO> listaCorVo = getCores(idCor);
            if (listaCorVo != null && listaCorVo.Count > 0)
                return listaCorVo[0];
            else
                return null;
        }
        
        public static List<CorVO> getCores()
        {
            return getCores(null);
        }
        
        public static List<CorVO> getCores(int? idCor)
        {
            string where = "WHERE ";
            StringBuilder query = new StringBuilder();

            query.AppendLine("SELECT IdCor, Nome, A, R, G, B, Ordem ");
            query.AppendLine("FROM cor ");
            if (idCor!=null){
                query.AppendLine(where + "IdCor=?IdCor ");
                where = " AND ";
            }
            query.AppendLine("ORDER BY Ordem, Nome ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            if (idCor!=null) cmd.Parameters.AddWithValue("?IdCor",idCor);

            MySqlDataReader reader = null;
            try
            {
                cmd.Connection.Open();

                reader = cmd.ExecuteReader();

                List<CorVO> listCor = new List<CorVO>();

                while (reader.Read())
                {
                    CorVO corVO = CorVO.CreateCorVO();
                    corVO.idCor = reader.GetInt32("IdCor");
                    corVO.nome = reader.GetString("Nome");
                    corVO.a = reader.GetInt32("A");
                    corVO.r = reader.GetInt32("R");
                    corVO.g = reader.GetInt32("G");
                    corVO.b = reader.GetInt32("B");
                    corVO.ordem = reader.GetInt32("Ordem");

                    listCor.Add(corVO);
                }
                reader.Close();
                return listCor;
            }
            catch (Exception ex){ throw ex; }
            finally { cmd.Connection.Close(); }
        }

        public static void setCor(CorVO corVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("UPDATE cor ");
            query.AppendLine("SET Nome=?Nome, A=?A, R=?R, G=?G, B=?B, Ordem=?Ordem ");
            query.AppendLine("WHERE IdCor=?IdCor ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdCor", corVO.idCor);
            cmd.Parameters.AddWithValue("?Nome", corVO.nome);
            cmd.Parameters.AddWithValue("?A", corVO.a);
            cmd.Parameters.AddWithValue("?R", corVO.r);
            cmd.Parameters.AddWithValue("?G", corVO.g);
            cmd.Parameters.AddWithValue("?B", corVO.b);
            cmd.Parameters.AddWithValue("?Ordem", corVO.ordem);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static long newCor(CorVO corVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("INSERT INTO cor ");
            query.AppendLine("SET Nome=?Nome, A=?A, R=?R, G=?G, B=?B, Ordem=?Ordem ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?Nome", corVO.nome);
            cmd.Parameters.AddWithValue("?A", corVO.a);
            cmd.Parameters.AddWithValue("?R", corVO.r);
            cmd.Parameters.AddWithValue("?G", corVO.g);
            cmd.Parameters.AddWithValue("?B", corVO.b);
            cmd.Parameters.AddWithValue("?Ordem", corVO.ordem);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
                return cmd.LastInsertedId;
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static void delCor(CorVO corVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("DELETE FROM cor ");
            query.AppendLine("WHERE IdCor=?IdCor ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdCor", corVO.idCor);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static bool readyToDeleteCor(int idCor)
        {
            int contagem = 0;
            StringBuilder query = new StringBuilder();
            query.AppendLine("SELECT count(*) contagem FROM veiculo WHERE IdCor=?IdCor ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();
            cmd.Parameters.AddWithValue("IdCor", idCor);

            MySqlDataReader reader = null;
            try
            {
                cmd.Connection.Open();

                reader = cmd.ExecuteReader();
                reader.Read();
                contagem = reader.GetInt32("contagem");
                reader.Close();
                return contagem==0;
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }
 
        }
    }
}
